<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('事件列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        审核
                    </h4>
                </div>
                <div class="modal-body">
                    <table class="table">
                        <thead>
                        <tr>
                            <th style="width: 100px;">流程名</th>
                            <th>流程描述</th>
                            <th>审核状态</th>
                        </tr>
                        </thead>
                        <tbody id="model-tbody">

                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>时间描述：</label>
                            <input type="text" name="eTime"/>
                        </li>
                        <li>
                            <label>地点：</label>
                            <input type="text" name="eLocation"/>
                        </li>
                        <li>
                            <label>申请人编号：</label>
                            <input type="text" name="iuId"/>
                        </li>
                        <li>
                            <label>审核：</label>
                            <input type="text" name="eAudit"/>
                        </li>
                        <li>
                            <label>进程：</label>
                            <input type="text" name="eProcess"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
<!--            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:icEvent:add">-->
<!--                <i class="fa fa-plus"></i> 添加-->
<!--            </a>-->
<!--            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"-->
<!--               shiro:hasPermission="system:icEvent:edit">-->
<!--                <i class="fa fa-edit"></i> 修改-->
<!--            </a>-->
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="system:icEvent:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:icEvent:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:icEvent:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:icEvent:remove')}]];
    var prefix = ctx + "system/icEvent";

    function getProcessName(code) {
        code = String(code)
        switch (code) {
            case "1":
                return "上报";
            case "2":
                return "受理";
            case "3":
                return "分配";
            case "4":
                return "办理";
            case "5":
                return "反馈";
            case "6":
                return "结案";
            default:
                return "";
        }
    }

    function passProcess(eid, processKey) {
        let eDesc = $('#inputEDesc').val()
        eDesc = eDesc ? eDesc : ''
        processKey = processKey ? processKey : 1
        if (processKey >= 7) {
            return
        }
        $.ajax({
            //url
            url: '/system/ic_event_process/event/pass',
            data: {
                eProcess: processKey,
                eId: String(eid),
                eDesc: eDesc,
                eAudit: 1,
            },
            //请求类型
            type: 'POST',
            //响应体类型
            dataType: 'json',
            success: resp => {
                if (resp.code === 0) {
                    $('#myModal').modal('hide')
                }
            }
        })
    }

    // 点击审核按钮
    function hello(elem) {
        let eid = $(elem).attr("value")
        if (!eid) {
            return
        }

        $.ajax({
            //url
            url: '/system/ic_event_process/event/' + eid,
            //请求类型
            type: 'GET',
            //响应体类型
            dataType: 'json',
            //请求成功的回调，参数是响应体
            success: function (resp) {
                if (resp.code !== 0) {
                    return
                }
                const data = resp.data
                console.log(data)
                let html = ''
                let processKey = 1
                if (data.processes) {
                    for (let p of data.processes) {
                        processKey = p.eProcess + 1
                        html += '<tr>\n' +
                            '<td>' + p.processName + '</td>\n' +
                            '<td>' + p.eDesc + '</td>\n' +
                            '<td>' + p.auditName + '</td>\n' +
                            '</tr>\n'
                    }
                }
                if (processKey < 7) {
                    html += '<tr>\n' +
                        '<td>' + getProcessName(processKey) + '</td>\n' +
                        '<td><input type="text" class="form-control" placeholder="请输入描述" id="inputEDesc"></td>\n' +
                        '<td><button type="button" class="btn btn-primary" onclick="passProcess(' + eid + ', ' + processKey + ')">通过</button></td>\n' +
                        '</tr>\n'
                }

                $("#model-tbody").html(html)
                $('#myModal').modal('show')
            },
            //超时时间
            timeout: 2000,
            //请求失败的回调，包括网络异常、请求超时
            error: function () {
                console.log("网络异常！！");
            },
        })
    }

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "事件",
            columns: [{
                checkbox: true
            },
                {
                    field: 'eId',
                    title: '编号',
                    visible: false
                },
                {
                    field: 'eTitle',
                    title: '标题'
                },
                {
                    field: 'eTime',
                    title: '时间描述'
                },
                {
                    field: 'eLocation',
                    title: '地点'
                },
                {
                    field: 'iuId',
                    title: '申请人编号'
                },
                {
                    field: 'eTypeName',
                    title: '申请类型'
                },
                {
                    field: 'eAuditName',
                    title: '审核状态'
                },
                {
                    field: 'eProcessName',
                    title: '当前进程'
                },
                {
                    field: 'info.eInfo',
                    title: '事件详情'
                },
                {
                    field: 'info.eContact',
                    title: '联系方式'
                },
                {
                    title: '操作',
                    align: 'center',
                    width: '150px',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="audit-btn btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" value="' + row.eId + '" onclick="hello(this)"><i class="fa fa-edit"></i>审核</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.eId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);

        // 点击tab按钮
        // $(".tab-btn").on('click', function(e) {
        //     $(".active").removeClass('active')
        //     $(this).addClass("active")
        // })
    });
</script>
</body>
</html>